package com.jmu.edu.IMApplication.fragment;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.android.material.button.MaterialButton;
import com.jmu.edu.IMApplication.GlobalEventListener;
import com.jmu.edu.IMApplication.MyApplication;
import com.jmu.edu.IMApplication.R;
import com.jmu.edu.IMApplication.adapter.FriendNotificationItemAdapter;
import com.jmu.edu.IMApplication.event.FriendNotificationRefresh;
import com.jmu.edu.IMApplication.utils.FriendUtils;
import com.jmu.edu.IMApplication.utils.ViewUtils;
import com.jmu.edu.IMApplication.utils.WillowUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.im.android.api.callback.GetUserInfoCallback;
import cn.jpush.im.android.api.event.ContactNotifyEvent;
import cn.jpush.im.android.api.event.MessageEvent;
import cn.jpush.im.android.api.model.Message;
import cn.jpush.im.android.api.model.UserInfo;

public class FriendNotificationFragment extends Fragment {
    private MaterialButton backButton;
    private RecyclerView recyclerView;
    private FriendNotificationItemAdapter adapter;
    private SwipeRefreshLayout swipeRefreshLayout;


    public FriendNotificationFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_friend_notification, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        EventBus.getDefault().register(this);
        //暂时在这里执行初始化，等数据库弄好以后用loadData直接获取
        initView();
        ViewUtils.addBackListenerForHide(getView(), backButton);


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            EventBus.getDefault().unregister(this);
        } else {
            EventBus.getDefault().register(this);
            ViewUtils.addBackListenerForHide(getView(), backButton);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefreshEvent(FriendNotificationRefresh friendNotificationRefresh) {
        if (friendNotificationRefresh.isRefresh()) {
            refreshData();
        }

    }


    private void initView() {
        backButton = getView().findViewById(R.id.fragment_friend_notification_back);
        swipeRefreshLayout = getView().findViewById(R.id.fragment_friend_notification_refresh_layout);
        initListener();
        recyclerView = getView().findViewById(R.id.fragment_friend_notification_recyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        //初始化adapter
        if (adapter == null) {
            adapter = new FriendNotificationItemAdapter();
        }
        adapter.setFriendInviteList(MyApplication.getFriendInviteList());
        recyclerView.setAdapter(adapter);

    }

    private void initListener() {
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ViewUtils.hideFragment(FriendNotificationFragment.this, R.anim.from_right, R.anim.out_right);
            }
        });
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshData();
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }


    private void refreshData() {
        if (adapter == null) {
            adapter = new FriendNotificationItemAdapter();
        }
        adapter.setFriendInviteList(MyApplication.getFriendInviteList());
        if (recyclerView.getAdapter() != null) {
            adapter.notifyDataSetChanged();
        } else {
            recyclerView.setAdapter(adapter);
        }
        Log.d("好友通知界面", "数据刷新,size:" + adapter.getItemCount());
    }

//
//    public void addData(String username, String reason, int state) {
//        WillowUtils.getUserInfo(username, new GetUserInfoCallback() {
//            @Override
//            public void gotResult(int i, String s, UserInfo userInfo) {
//                if (i == 0) {
//                    userInfoList.add(userInfo);
//                    stateList.add(state);
//                    Log.d("好友监听", "添加数据:" + userInfo.getUserName() + "\n现在数据列表size：" + userInfoList.size());
//
//                } else {
//                    Log.d("好友监听", "添加数据失败，没有找到该用户");
//                }
//            }
//        });
//    }
}